package com.phpxiu.app.pulltorefresh;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.pulltorefresh.PullToRefreshView.Mode;
import com.phpxiu.app.pulltorefresh.PullToRefreshView.Orientation;

/**
 * @file
 * @brief 下拉刷新上拉加载控件，底部自定义视图
 * @author 吴金洲
 * @note  下拉刷新上拉加载控件，底部自定义视图
 *
 */
public class FooterLoadingLayout extends LoadingLayout {
    private FrameLayout mInnerLayout;
    private final TextView mFooterText;
    protected final Mode mMode;
    protected final Orientation mScrollDirection;
    private CharSequence mPullLabel;
    private CharSequence mRefreshingLabel;
    private CharSequence mReleaseLabel;

    public FooterLoadingLayout(Context context, Mode mode, Orientation scrollDirection, TypedArray attrs) {
        super(context, mode, scrollDirection, attrs);
        mMode = mode;
        mScrollDirection = scrollDirection;
        LayoutInflater.from(context).inflate(R.layout.pull_to_refresh_footer, this);
        mInnerLayout = (FrameLayout) findViewById(R.id.fl_inner);
        mFooterText = (TextView) mInnerLayout.findViewById(R.id.pull_to_refresh_text);
        LayoutParams lp = (LayoutParams) mInnerLayout.getLayoutParams();
        switch (mode) {
            case PULL_FROM_END:
                lp.gravity = scrollDirection == Orientation.VERTICAL ? Gravity.TOP : Gravity.LEFT;
                // Load in labels
                // Load in labels
                mPullLabel = context.getString(R.string.pull_to_refresh_pull_label);
                mRefreshingLabel = context.getString(R.string.pull_to_refresh_refreshing_label);
                mReleaseLabel = context.getString(R.string.pull_to_refresh_release_label);
                break;
            case PULL_FROM_START:
            default:
                lp.gravity = scrollDirection == Orientation.VERTICAL ? Gravity.BOTTOM : Gravity.RIGHT;
                break;
        }

        if (attrs.hasValue(R.styleable.PullToRefresh_ptrHeaderBackground)) {
            Drawable background = attrs.getDrawable(R.styleable.PullToRefresh_ptrHeaderBackground);
            if (null != background) {
                PullToRefreshHelper.setBackground(this, background);
            }
        }

        if (attrs.hasValue(R.styleable.PullToRefresh_ptrHeaderTextAppearance)) {
            TypedValue styleID = new TypedValue();
            attrs.getValue(R.styleable.PullToRefresh_ptrHeaderTextAppearance, styleID);
            setTextAppearance(styleID.data);
        }

        // Text Color attrs need to be set after TextAppearance attrs
        if (attrs.hasValue(R.styleable.PullToRefresh_ptrHeaderTextColor)) {
            ColorStateList colors = attrs.getColorStateList(R.styleable.PullToRefresh_ptrHeaderTextColor);
            if (null != colors) {
                setTextColor(colors);
            }
        }
        reset();
    }

    public final void setHeight(int height) {
        ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) getLayoutParams();
        lp.height = height;
        requestLayout();
    }

    public final void setWidth(int width) {
        ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) getLayoutParams();
        lp.width = width;
        requestLayout();
    }

    public final int getContentSize() {
        return mInnerLayout.getHeight();
    }

    @Override
    public void hideAllViews() {
        if (View.VISIBLE == mFooterText.getVisibility()) {
            mFooterText.setVisibility(View.INVISIBLE);
        }
    }

    public final void onPull(float scaleOfLayout) {
        //TODO nothing
    }

    public void pullToRefresh() {
        if (null != mFooterText) {
            mFooterText.setText(mPullLabel);
        }

        // Now call the callback
        pullToRefreshImpl();
    }

    @Override
    public void refreshing() {
        super.refreshing();
        if (null != mFooterText) {
            mFooterText.setText(mRefreshingLabel);
        }
        refreshingImpl();
    }

    public final void releaseToRefresh() {
        if (null != mFooterText) {
            mFooterText.setText(mReleaseLabel);
        }

        // Now call the callback
        releaseToRefreshImpl();
    }

    @Override
    public final void reset() {
        if (null != mFooterText) {
            mFooterText.setText(mPullLabel);
        }
        // Now call the callback
        resetImpl();
    }

    @Override
    public void setLastUpdatedLabel(CharSequence label) {

    }

    private void setTextAppearance(int value) {
        if (null != mFooterText) {
            mFooterText.setTextAppearance(getContext(), value);
        }
    }

    private void setTextColor(ColorStateList color) {
        if (null != mFooterText) {
            mFooterText.setTextColor(color);
        }
    }

    public void setPullLabel(CharSequence pullLabel) {
        mPullLabel = pullLabel;
    }

    public void setRefreshingLabel(CharSequence refreshingLabel) {
        mRefreshingLabel = refreshingLabel;
    }

    public void setReleaseLabel(CharSequence releaseLabel) {
        mReleaseLabel = releaseLabel;
    }

    @Override
    public void setTextTypeface(Typeface tf) {
        mFooterText.setTypeface(tf);
    }

    @Override
    public final void showInvisibleViews() {
        if (View.INVISIBLE == mFooterText.getVisibility()) {
            mFooterText.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void setLoadingDrawable(Drawable drawable) {
        //TODO nothing
    }

    public void onLoadingDrawableSet(Drawable imageDrawable) {
        //TODO nothing
    }

    protected void onPullImpl(float scaleOfLayout) {
       //TODO nothing
    }

    @Override
    protected void refreshingImpl() {

    }

    @Override
    protected void resetImpl() {
        resetImageRotation();
    }

    private void resetImageRotation() {

    }

    @Override
    protected void pullToRefreshImpl() {
        // NO-OP
    }

    @Override
    protected void releaseToRefreshImpl() {
        // NO-OP
    }

    @Override
    protected int getDefaultDrawableResId() {
        return R.drawable.gray_loading;
    }

}
